<?PHP
/**
 * 控制器处理类 单页
 */
class Admin_page_Controller extends controller_admin{
	function __construct(){
		parent::__construct();
	 	$this->assign('nav','单页信息管理');
		$this->assign('menuid1','1');
		$this->assign('menuid2','5');
		//权限判断
		factory::module('admin')->authSys('page'); 
	}
	
	##################################
	/* 单页信息管理  */
	##################################
	function index__(){
    
	}
	function index_list__(){
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{
			#得到传递过来的参数
			$_POST=format::addslash_deep($_POST);
			if(!isset($_POST['start']))			$_POST['start']			= 0;
			if(!isset($_POST['limit']))			$_POST['limit']			= 10;
			if(!isset($_POST['sort']))				$_POST['sort']			= 'a.id';
			if(!isset($_POST['dir']))				$_POST['dir']				= 'DESC';
			if(!isset($_POST['keywords']))   $_POST['keywords']  = '';    
  	  #查询条件
			$sql_where = ' where    a.deleted=0 '.func::speedsearch($_POST['keywords'],array( 'a.title','a.content' ));
			#排序信息
			$sql_order = " ORDER BY ".$_POST['sort'].' '.$_POST['dir']." LIMIT ".$_POST['start'].", ".$_POST['limit'];
			#先查总数 
			$sql = "
			select 
			count(a.id) 
			from `t_page` a  
			".$sql_where ;  
			$db = factory::getDBO();
			$rows = $db->LoadResult($sql); 
			if($rows){
				#检索出具体数据
				$sql = "select
					a.id, 
					a.title,
					a.update_date
				from `t_page` a 
				".$sql_where.$sql_order;
				$rs = $db->LoadAssocList($sql);
				#输出结果
				echo '({"total":"'.$rows.'","results":'.json_encode($rs).'})';
			}else{
				echo '({"total":"0","results":[]})';
			}
			exit();  
		}  
	} 
	# 浏览
	public function  view__(){ 
		   
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_page` a  where deleted=0 and a.id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		$this->assign('rs',$rs);  
		//exit(htmlspecialchars_decode($rs['content']));  // 将转义的字符反转过来显示
	}
	# 修改
	public function  modify__(){   
		if (strtoupper($_SERVER['REQUEST_METHOD']) == 'POST')
		{ 	 
			try{ 
				$form = format::addslash_json(json_decode( stripslashes($_POST['form']) ));  
			  $form['content'] = htmlspecialchars(rawurldecode($form['content']));  //做转义存储，防止sql注入
			}catch(Exception $e){
				exit('参数错误');
			}
			$id = $_POST['id'];
			$db = factory::getDBO(); 
			$db->begin(); 
			//主表
		  $db->update('page',array(
				'id'=>$form['id'], 
				'content'=>$form['content'],
				'update_date'=>'now()',
				'update_user_id'=>factory::module('admin')->getuserid()
			));
			$db->commit();
			exit('{success:1,msg:"修改成功！"}');
		} 
		$this->assign('menuid1','3');
		$this->assign('nav','单页信息管理 - 修改信息');  
		
		$_GET=format::addslash_deep($_GET);
		$id = intval($_GET['id']);
		$db = factory::getDBO();
		$rs = $db->loadassoc('select a.* from `t_page` a  where deleted=0 and a.id="'.$id.'" ');
		if(!$rs) exit('没有查到任何记录，请重新操作！'); 
		
		$this->assign('rs',$rs);
	}
}
?>